package com.bsj.media.network.tcpNetty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

public class NettySendUntil {

    public static void send(ChannelHandlerContext ctx, byte[] data) {
        final ByteBuf buf = PooledByteBufAllocator.DEFAULT.heapBuffer(data.length);
        buf.writeBytes(data);
        final Channel channel = ctx.channel();
        if (channel.eventLoop().inEventLoop()) {
            channel.writeAndFlush(buf);
        } else {
            channel.eventLoop().execute(new Runnable() {
                @Override
                public void run() {
                    channel.writeAndFlush(buf);
                }
            });
        }
    }
}
